package com.wj.rspmproperty.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wj.rspmproperty.mapper.BuildingMapper;
import com.wj.rspmproperty.pojo.Building;
import com.wj.rspmproperty.pojo.PageResult;
import com.wj.rspmproperty.service.BuildingService;
import com.wj.rspmproperty.uitls.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;



/**
 * @author WeiJin
 * @version 1.0
 */
@Service
@Transactional
public class BuildingServiceImpl implements BuildingService {


    @Autowired
    private BuildingMapper buildingMapper;

    /**
     * 查询在使用的建筑五、物
     *
     * @return
     */
    @Override
    public List<Building> buildingOfUsed() {
        return buildingMapper.buildingOfStatus(1);
    }

    /**
     * 新增楼栋
     *
     * @param building
     * @return
     */
    @Override
    public int addBuilding(Building building) {
        return buildingMapper.addBuilding(building);
    }

    /**
     * 分页查询楼栋
     *
     * @param current
     * @param size
     * @return
     */
    @Override
    public PageResult<Building> pageOfBuilding(Integer current, Integer size) {
        PageHelper.startPage(current, size);
        return PageResult.restPage((Page<Building>) buildingMapper.buildingOfStatus(0));
    }

    /**
     * 批量添加建筑物
     *
     * @param building     建筑模版
     * @param buildings    建筑集合
     * @param buildingSize 新增个数
     * @return
     */
    @Override
    public Integer insertBuildings(Building building, List<Building> buildings, Integer buildingSize) {
        //先将模版添加进需要新增的集合
        buildings.add(building);
        int result = -1;

        try {
            for (int i = 0; i < buildingSize; i++) {
                //克隆
                Building tempBuilding = building.clone();
                //处理楼栋号避免重名
                String code = building.getBuilding_code();
                code = code.replaceAll("\\d+", StringUtil.numberToLen2String(StringUtil.getNumberFromString(code)+i+1));
                tempBuilding.setBuilding_code(code);
                //处理楼栋名避免重名
                String name = building.getBuilding_name();
                name = name.replaceAll("\\d+",StringUtil.numberToLen2String(StringUtil.getNumberFromString(name)+i+1));
                tempBuilding.setBuilding_name(name);
                buildings.add(tempBuilding);
            }
            result = buildingMapper.insertBuildings(buildings);
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        } catch (DuplicateKeyException e){
          e.printStackTrace();
          throw new DuplicateKeyException("建筑编号重复"+e.getMessage());

        }

        return result;
    }

    @Override
    public int changeBuildingsStatus(List<Integer> idList, Integer status) {
        return buildingMapper.changeBuildingsStatus(idList,status);
    }
}
